﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace He.Common
{
    /// <summary>
    /// Cookie操作辅助类
    /// </summary>
    public class CookieHelper
    {
        #region 写入Cookie
        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="cookieValue">cookie值</param>
        public static void Set(string cookieName, string cookieValue)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie == null)
            {
                cookie = new HttpCookie(cookieName);
            }
            cookie.Value = cookieValue;
            cookie.Expires = DateTime.MaxValue;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="cookieValue">cookie值</param>
        /// <param name="expires">cookie过期时间[以秒为单位]</param>
        public static void Set(string cookieName, string cookieValue, int expires)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie == null)
            {
                cookie = new HttpCookie(cookieName);
            }
            cookie.Value = cookieValue;
            cookie.Expires = DateTime.Now.AddSeconds(expires);
            HttpContext.Current.Response.AppendCookie(cookie);
        }
        #endregion

        #region 写入Cookie(带关联域)
        /// <summary>
        /// 带关联域写cookie值
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="cookieValue">cookie值</param>
        /// <param name="domain">与Cookie关联的域</param>
        public static void Set(string cookieName, string cookieValue, string domain)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie == null)
            {
                cookie = new HttpCookie(cookieName);
            }
            cookie.Domain = domain;
            cookie.Value = cookieValue;
            cookie.Expires = DateTime.MaxValue;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 带关联域写cookie值
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="cookieValue">cookie值</param>
        /// <param name="expires">cookie过期时间[以分钟为单位]</param>
        /// <param name="domain">作用域</param>
        public static void Set(string cookieName, string cookieValue
            , int expires, string domain)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie == null)
            {
                cookie = new HttpCookie(cookieName);
            }
            cookie.Domain = domain;
            cookie.Value = cookieValue;
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            HttpContext.Current.Response.AppendCookie(cookie);
        }
        #endregion

        #region 写入Cookie(带关联域，并设置是否跨域名)
        /// <summary>
        /// 写入Cookie(带关联域，并设置是否跨域名)
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="cookieValue">cookie值</param>
        /// <param name="domain">作用域</param>
        /// <param name="isCrossDomain">此Cookies是否支持跨域访问</param>
        public static void Set(string cookieName, string cookieValue, string domain, bool isCrossDomain)
        {
            //跨域 com,de
            //
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie == null)
            {
                cookie = new HttpCookie(cookieName);
            }
            cookie.Domain = domain;
            cookie.Value = cookieValue;
            cookie.Expires = DateTime.MaxValue;
            if (isCrossDomain)
            {
                HttpContext.Current.Response.AddHeader("P3P", " CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR'");
            }
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 写入Cookie(带关联域，并设置是否跨域名)
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="cookieValue">cookie值</param>
        /// <param name="expires">cookie过期时间[以分钟为单位]</param>
        /// <param name="domain">作用域</param>
        /// <param name="isCrossDomain">此Cookies是否支持跨域访问</param>
        public static void Set(string cookieName, string cookieValue
            , int expires, string domain, bool isCrossDomain)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie == null)
            {
                cookie = new HttpCookie(cookieName);
            }
            cookie.Domain = domain;
            cookie.Value = cookieValue;
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            if (isCrossDomain)
            {
                HttpContext.Current.Response.AddHeader("P3P", " CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR'");
            }
            HttpContext.Current.Response.AppendCookie(cookie);
        }
        #endregion

        #region 获取Cookie值
        /// <summary>
        /// 读cookie值
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <returns>cookie值</returns>
        public static string Get(string cookieName)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[cookieName] != null && HttpContext.Current.Request.Cookies[cookieName].Value != null)
            {
                return HttpContext.Current.Request.Cookies[cookieName].Value;
            }

            return string.Empty;
        }

        /// <summary>
        /// 读cookie值
        /// </summary>
        /// <param name="cookieName">cookie名称</param>
        /// <param name="isCrossDomain">是否获取跨域的Cookies</param>
        /// <returns>cookie值</returns>
        public static string Get(string cookieName, bool isCrossDomain)
        {
            if (isCrossDomain)
            {
                HttpContext.Current.Response.AddHeader("P3P", " CP='CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR'");
            }
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[cookieName] != null && HttpContext.Current.Request.Cookies[cookieName].Value != null)
            {
                return HttpContext.Current.Request.Cookies[cookieName].Value;
            }

            return string.Empty;
        }
        #endregion

        #region 移除Cookie
        /// <summary>
        /// 移除Cookie
        /// </summary>
        /// <param name="cookieName">Cookie名称</param>
        public static void Remove(string cookieName)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[cookieName] != null)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(cookieName);
                }
                cookie.Expires = DateTime.Now.AddMinutes(-1);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
        #endregion
    }
}
